Repository navigation
Conversation
Coalesce refreshes for the current token source and let each OAuth caller wait with its own context. Wake waiters when a newer authorization supplies a token and keep bearer lookup cancellable across the stdio GitHub clients.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The tool guard lacks a cancellation regression test, leaving ignored HasTokenContext errors or use of a non-cancelable context undetected.
1 open issue
What changed in this PR
Makes stdio OAuth token-refresh waits cancellable while allowing the shared refresh to finish and preserve rotated credentials for the session.
Changes:
- Shares in-flight refreshes and wakes waiters when a newer authorization installs a token.
- Passes request contexts through the tool guard and REST, GraphQL, and raw-content token lookups.
- Adds cancellation and transport regression tests, and documents refresh ownership.
| File | Description |
|---|---|
internal/oauth/refresh_cancellation_test.go |
Tests cancellation, shared refreshes, and authorization replacement. |
internal/oauth/manager.go |
Adds context-aware token waits and shared-refresh coordination. |
internal/ghmcp/server.go |
Connects request contexts to outbound OAuth bearer lookups. |
internal/ghmcp/server_test.go |
Updates client setup for the OAuth provider parameter. |
internal/ghmcp/oauth.go |
Makes the tool guard’s token check cancellable. |
internal/ghmcp/oauth_test.go |
Updates authentication fakes and client setup. |
internal/ghmcp/oauth_refresh_transport_test.go |
Tests request cancellation, body closure, and host-scoped bearer handling. |
docs/oauth-login.md |
Explains shared refreshes and canceled request waits. |
Review effort: Max (adaptive)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Let canceled stdio MCP requests stop waiting for an OAuth token refresh. Concurrent requests can still receive the result of the same refresh and reuse the rotated credentials.
Why
When the session token expired, the stdio tool guard called
HasToken, which synchronously calledoauth2.TokenSource.Token. Authorization resume and the outbound bearer provider could enter the same path. The source uses an independent HTTP context for refresh. That is appropriate for the shared exchange, but it also meant a canceled caller could not return until the exchange released it.A gated token source reproduced the problem on
df32d7ec5786. BothAuthenticateandAwaitTokenremained blocked after cancellation while the refresh was held. An already-canceled call entered the source and returned success instead ofcontext.Canceled. Installing a newer authorization also left waiters blocked on the old source.What changed
MCP impact
Prompts tested (tool changes only)
The authorization tests use synthetic tokens and a gated refresh source. The outbound tests use the configured clients against a local HTTP fixture for REST, GraphQL, and raw content.
Security / limits
Tool renaming
Lint & tests
./script/linton Linux with golangci-lint v2.14.0../script/test(go test -race ./...) on Linux.go test -v ./..., the snapshot suite withUPDATE_TOOLSNAPS=true, and a CLI build. The verbose suite passed 32 packages, 1,225 top-level tests, and 7,997 subtests. The new OAuth manager and stdio transport tests each passed 100 consecutive runs. The tagged end-to-end package compiled with execution disabled.go mod tidy -diff, a CLI build, and regenerated documentation validation.Docs
Skills
Related work includes the stdio OAuth introduction, multi-round-trip OAuth migration, and GitHub's refresh token rotation documentation.
AI assistance was used to investigate and reproduce the defect, author the fix and tests, review the diff, and draft this description.